package sample;

import javafx.animation.*;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;


import java.util.ArrayList;
import java.util.List;

/**
 * 游戏主控
 *
 * @Description:
 * @ClassName: Controller
 * @Author: Yl-J
 * @Date: 2020/03/22
 */
@SuppressWarnings("Duplicates")
public class Controller implements EventHandler<KeyEvent> {

    /**
     * 游戏区域
     */
    @FXML
    private Pane playArea;

    /**
     *
     */
    @FXML
    private Label tbScore;

    /**
     * 滑动条
     */
    @FXML
    private Slider sliderSize;

    /**
     *
     */
    @FXML
    private Label tbSize;

    /**
     * 按键 休止
     */
    @FXML
    private Button btnReset;

    /**
     * 长度
     */
    public int size;

    /**
     * 表格
     */
    private int[][] table;

    /**
     * 上一步表
     */
    private int[][] tableold;

    /**
     *
     */
    private Point[][] tableFlight;

    /**
     *
     */
    private int[][] tableAfter;

    /**
     * 表格添加-新数字
     */
    private Point[] tableAdd = new Point[2];

    /**
     *
     */
    private State state;

    /**
     * 按下键盘
     */
    private boolean keyPressed;

    /**
     *
     */
    private boolean startFlag = false;

    /**
     * 键盘编码
     */
    private KeyCode code;

    /**
     * 隔窗组
     */
    private List<Pane> paneList = new ArrayList<>();

    /**
     * 长
     */
    public double width;

    /**
     * 宽
     */
    public double height;

    /**
     * 外边距
     */
    public int margin = 0;

    /**
     * 边框
     */
    public int scoreInt = 0;

    /**
     *
     */
    public long mLastSize = 0;

    /**
     * 按下鼠标
     */
    private boolean mMousePressed;

    /**
     * 字体库
     */
    private final String fontName = "System";

    /**
     *
     */
    SimpleStringProperty score = new SimpleStringProperty("0");

    /**
     * 颜色组
     */
    private Color[] color = new Color[]{
            Color.TRANSPARENT,
            Color.rgb(255, 236, 158),
            Color.rgb(255, 193, 0),
            Color.rgb(255, 81, 0),
            Color.rgb(255, 0, 92),
            Color.rgb(185, 0, 255),
            Color.rgb(122, 0, 255),
            Color.rgb(0, 60, 255),
            Color.rgb(0, 207, 255),
            Color.rgb(0, 255, 99),
            Color.rgb(126, 255, 0),
            Color.rgb(190, 255, 0),
    };

    /**
     *
     */
    public Transition m2048ColorTransition;

    /**
     * 按下 得分
     */
    private Point mPressedPoint;

    /**
     * 释放 得分
     */
    private Point mReleasedPoint;

    /**
     * 初始化
     */
    public void Init() {
        tbSize.setText("4");
        mLastSize = 4;
        tbScore.textProperty().bind(score);
        sliderSize.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                long n = Math.round(sliderSize.getValue());
                sliderSize.setValue(n);
                if (n != mLastSize) {
                    mLastSize = n;
                    ImplInit((int) n);
                }
                playArea.requestFocus();
            }
        });
        sliderSize.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                long o = Math.round(oldValue.doubleValue());
                long n = Math.round(newValue.doubleValue());
                if (o != n) {
                    tbSize.setText(String.format("%d", n));

                }
            }
        });
        btnReset.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    playArea.requestFocus();
                }
            }
        });
    }

    /**
     * 初始化-图片
     *
     * @Description:
     * @Param: [size]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void ImplInit(int size) {
        this.size = size;
        startFlag = true;
        scoreInt = 0;
        score.setValue("0");
        state = State.RUNNING;
        table = new int[size][size];
        tableFlight = new Point[size][size];
        tableAfter = new int[size][size];
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                tableFlight[i][j] = new Point(0, 0);
            }
        playArea.getChildren().clear();
        paneList.clear();
        width = playArea.getWidth();
        height = playArea.getHeight();
        Rectangle bk = new Rectangle();
        if (size == 3) {
            margin = 20;
        }
        if (size == 4) {
            margin = 15;
        }
        if (size == 5) {
            margin = 10;
        }
        if (size == 6) {
            margin = 6;
        }
        bk.setWidth(width - margin / 2);
        bk.setHeight(height - margin / 2);
        bk.setFill(Color.grayRgb(195));
        bk.setArcWidth(10);
        bk.setArcHeight(10);
        playArea.getChildren().add(bk);

        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                StackPane pane = new StackPane();
                Rectangle r = new Rectangle();
                r.setFill(Color.grayRgb(225));
                r.setStroke(Color.TRANSPARENT);
                pane.setLayoutX(j * (width - margin) / size + margin / 2 - 1);
                pane.setLayoutY(i * (height - margin) / size + margin / 2 - 1);
                pane.setPrefWidth((width - margin) / size - margin / 2 + 2);
                pane.setPrefHeight((height - margin) / size - margin / 2 + 2);
                r.setWidth((width - margin) / size - margin / 2 + 2);
                r.setHeight((height - margin) / size - margin / 2 + 2);
                pane.getChildren().add(r);
                playArea.getChildren().add(pane);
            }
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                StackPane pane = new StackPane();
                Rectangle r = new Rectangle();
                r.setFill(color[0]);
                r.setStroke(Color.TRANSPARENT);
                r.setArcHeight(30);
                r.setArcWidth(30);
                Text text = new Text("");
                text.setFill(Color.BLACK);
                if (size == 3) {
                    text.setFont(new Font(fontName, 45));
                    r.setArcHeight(40);
                    r.setArcWidth(40);
                }
                if (size == 4) {
                    text.setFont(new Font(fontName, 40));
                    r.setArcHeight(30);
                    r.setArcWidth(30);
                }
                if (size == 5) {
                    text.setFont(new Font(fontName, 35));
                    r.setArcHeight(20);
                    r.setArcWidth(20);
                }
                if (size == 6) {
                    text.setFont(new Font(fontName, 25));
                    r.setArcHeight(10);
                    r.setArcWidth(10);
                }

                pane.setLayoutX(j * (width - margin) / size + margin / 2);
                pane.setLayoutY(i * (height - margin) / size + margin / 2);
                pane.setPrefWidth((width - margin) / size - margin / 2);
                pane.setPrefHeight((height - margin) / size - margin / 2);
                r.setWidth((width - margin) / size - margin / 2);
                r.setHeight((height - margin) / size - margin / 2);
                pane.getChildren().add(r);
                pane.getChildren().add(text);
                paneList.add(pane);
                playArea.getChildren().add(pane);

            }

        AfterMove();
    }

    /**
     * 渲染
     *
     * @Description:
     * @Param: []
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void Render() {
        int add = 1;
        if (startFlag) {
            add = 2;
            startFlag = false;
        }
        ParallelTransition parallelTransition2 = new ParallelTransition();
        List<MoveAnime> list = new ArrayList<>();
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                StackPane pane = (StackPane) paneList.get(i * size + j);
                if (tableFlight != null) {
                    if (tableFlight[i][j].my != 0 || tableFlight[i][j].mx != 0) {
                        list.add(new MoveAnime(this, pane, tableFlight[i][j].mx, tableFlight[i][j].my));
                    }
                }
                if (tableAfter != null && tableAfter[i][j] != 0) {
                    ScaleTransition scaleTransition = new ScaleTransition(Duration.millis(100), pane);
                    scaleTransition.setFromX(1.15);
                    scaleTransition.setFromY(1.15);
                    scaleTransition.setToX(1);
                    scaleTransition.setToY(1);
                    parallelTransition2.getChildren().add(scaleTransition);
                }

                for (int k = 0; k < add; k++) {
                    if (i == tableAdd[k].mx && j == tableAdd[k].my) {
                        ScaleTransition scaleTransition = new ScaleTransition(Duration.millis(100), pane);
                        scaleTransition.setFromX(0.85);
                        scaleTransition.setFromY(0.85);
                        scaleTransition.setToX(1);
                        scaleTransition.setToY(1);
                        parallelTransition2.getChildren().add(scaleTransition);
                    }
                }
            }
        StartMoveAnime(list);
        SequentialTransition s = new SequentialTransition();
        PauseTransition pauseTransition = new PauseTransition(Duration.millis(100));
        s.getChildren().addAll(pauseTransition, parallelTransition2);
        s.play();
    }

    /**
     * 开始移动动画
     *
     * @Description:
     * @Param: [list]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void StartMoveAnime(List<MoveAnime> list) {
        Animation animation = new Transition() {
            {
                setCycleDuration(Duration.millis(100));
            }

            @Override
            protected void interpolate(double frac) {
                for (MoveAnime item : list) {
                    item.mPane.setLayoutX(item.mFromX + frac * item.mMoveX);
                    item.mPane.setLayoutY(item.mFromY + frac * item.mMoveY);
                }
            }
        };
        animation.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                for (int i = 0; i < size; i++)
                    for (int j = 0; j < size; j++) {
                        StackPane pane = (StackPane) paneList.get(i * size + j);
                        pane.setLayoutX(j * (width - margin) / size + margin / 2);
                        pane.setLayoutY(i * (height - margin) / size + margin / 2);
                        Rectangle r = (Rectangle) pane.getChildren().get(0);
                        Text t = (Text) pane.getChildren().get(1);
                        r.setFill(color[table[i][j]]);
                        if (table[i][j] != 0) {
                            t.setText(String.format("%d", (int) Math.pow(2, table[i][j])));
                        } else {
                            t.setText("");
                        }
                    }
            }
        });
        animation.play();
    }

    /**
     * 添加随机块
     *
     * @Description:
     * @Param: []
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void AddRandomBlock() {
        int add = 1;
        if (startFlag) {
            add = 2;
        }
        List<Integer> list = CheckEmptyArea();
        if (list.size() != 0)
            for (int j = 0; j < add; j++) {
                int i = (int) (Math.random() * list.size());
                int k = list.get(i);
                table[k / size][k % size] = 1;
                list.remove(i);
                tableAdd[j] = new Point(k / size, k % size);

            }
    }

    /**
     * 检查空的区域
     *
     * @Description:
     * @Param: []
     * @return: java.util.List<java.lang.Integer>
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private List<Integer> CheckEmptyArea() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                if (table[i][j] == 0)
                    list.add(i * size + j);
            }
        return list;
    }

    /**
     * 按下键盘
     *
     * @Description:
     * @Param: [keyEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnKeyPressed(KeyEvent keyEvent) {
        if (!keyPressed) {
            keyPressed = true;
            code = keyEvent.getCode();
        }
    }

    /**
     * 释放键盘
     *
     * @Description:
     * @Param: [event]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void OnKeyReleased(KeyEvent event) {
        if (keyPressed) {
            keyPressed = false;
            if (event.getCode() == code) {
                ProcessCode(code);
            }
        }
    }

    /**
     * 变更操作
     *
     * @Description:
     * @ClassName: Controller
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private boolean OnChange() {
        if (state == State.RUNNING) {
            for (int i = 0; i < size; i++)
                for (int j = 0; j < size; j++) {
                    if (table[i][j] != tableold[i][j]) {
                        return true;
                    }
                }
        }
        return false;
    }

    /**
     * 按下键
     *
     * @Description:
     * @Param: [code]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void ProcessCode(KeyCode code) {
        tableold = new int[size][size];
        tableAfter = new int[size][size];
        tableFlight = new Point[size][size];
        for (int i = 0; i < size; i++) {
            System.arraycopy(table[i], 0, tableold[i], 0, size);
        }
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                tableFlight[i][j] = new Point(0, 0);
                tableAfter[i][j] = 0;
            }
        }
        int[] l;
        switch (code) {
            case UP:
                l = new int[size];
                for (int i = 0; i < size; i++)
                    l[i] = 0;
                for (int i = 1; i < size; i++) {
                    for (int j = 0; j < size; j++) {
                        if (table[i][j] != 0) {
                            int k = i - 1;
                            for (; k >= l[j]; k--) {
                                if (table[k + 1][j] == table[k][j]) {
                                    table[k + 1][j] = 0;
                                    table[k][j]++;
                                    l[j] = k + 1;
                                    tableFlight[i][j].my--;
                                    tableAfter[k][j] = 1;
                                } else if (table[k + 1][j] != 0 && table[k][j] == 0) {
                                    table[k][j] = table[k + 1][j];
                                    table[k + 1][j] = 0;
                                    tableFlight[i][j].my--;
                                }
                            }

                        }
                    }
                }
                if (OnChange())
                    AfterMove();
                break;
            case DOWN:
                l = new int[size];
                for (int i = 0; i < size; i++)
                    l[i] = size - 1;
                for (int i = size - 2; i >= 0; i--) {
                    for (int j = 0; j < size; j++) {
                        if (table[i][j] != 0) {
                            int k = i + 1;
                            for (; k <= l[j]; k++) {
                                if (table[k - 1][j] == table[k][j]) {
                                    table[k - 1][j] = 0;
                                    table[k][j]++;
                                    l[j] = k - 1;
                                    tableFlight[i][j].my++;
                                    tableAfter[k][j] = 1;
                                } else if (table[k - 1][j] != 0 && table[k][j] == 0) {
                                    table[k][j] = table[k - 1][j];
                                    table[k - 1][j] = 0;
                                    tableFlight[i][j].my++;
                                }
                            }
                        }
                    }
                }
                if (OnChange())
                    AfterMove();
                break;
            case LEFT:
                l = new int[size];
                for (int i = 0; i < size; i++)
                    l[i] = 0;
                for (int j = 1; j < size; j++) {
                    for (int i = 0; i < size; i++) {
                        if (table[i][j] != 0) {
                            int k = j - 1;
                            for (; k >= l[i]; k--) {
                                if (table[i][k] == table[i][k + 1]) {
                                    table[i][k + 1] = 0;
                                    table[i][k]++;
                                    l[i] = k + 1;
                                    tableFlight[i][j].mx--;
                                    tableAfter[i][k] = 1;
                                } else if (table[i][k + 1] != 0 && table[i][k] == 0) {
                                    table[i][k] = table[i][k + 1];
                                    table[i][k + 1] = 0;
                                    tableFlight[i][j].mx--;
                                }
                            }

                        }
                    }
                }
                if (OnChange())
                    AfterMove();
                break;
            case RIGHT:
                l = new int[size];
                for (int i = 0; i < size; i++)
                    l[i] = size - 1;
                for (int j = size - 2; j >= 0; j--) {
                    for (int i = 0; i < size; i++) {
                        if (table[i][j] != 0) {
                            int k = j + 1;
                            for (; k <= l[i]; k++) {
                                if (table[i][k] == table[i][k - 1]) {
                                    table[i][k - 1] = 0;
                                    table[i][k]++;
                                    l[i] = k - 1;
                                    tableFlight[i][j].mx++;
                                    tableAfter[i][k] = 1;
                                } else if (table[i][k - 1] != 0 && table[i][k] == 0) {
                                    table[i][k] = table[i][k - 1];
                                    table[i][k - 1] = 0;
                                    tableFlight[i][j].mx++;
                                }
                            }

                        }
                    }
                }
                if (OnChange())
                    AfterMove();
                break;
        }

    }

    /**
     * 移动后
     *
     * @Description:
     * @Param: []
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void AfterMove() {
        AddRandomBlock();
        AddScore();
        if (CheckLose())
            state = State.GAMEOVER;
        if (Check2048())
            state = State.GAMESUCCESS;

        Render();
        if (state == State.GAMEOVER) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("抱歉");
            alert.setHeaderText("游戏结束");
            alert.setContentText("我去买几个橘子，你就站在此地不要走动。\n请点击按钮重置游戏！");
            alert.show();
        } else if (state == State.GAMESUCCESS) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("666");
            alert.setHeaderText("大吉大利，晚上吃鸡");
            alert.setContentText("你赢了？--鲁迅。\n请点击按钮重置游戏！");
            alert.show();
        }
    }

    /**
     * 检查
     *
     * @Description:
     * @Param: []
     * @return: boolean
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private boolean Check2048() {
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                if (table[i][j] == 11) {
                    StackPane pane = (StackPane) paneList.get(i * size + j);
                    Rectangle rectangle = (Rectangle) pane.getChildren().get(0);
                    Timeline timeline = new Timeline();
                    KeyValue[] values = new KeyValue[]{
                            new KeyValue(rectangle.fillProperty(), Color.rgb(255, 0, 0)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(255, 125, 0)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(255, 255, 0)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(0, 255, 0)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(0, 255, 255)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(0, 0, 255)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(255, 0, 255)),
                            new KeyValue(rectangle.fillProperty(), Color.rgb(255, 0, 0))
                    };
                    KeyFrame[] frames = new KeyFrame[]{
                            new KeyFrame(Duration.millis(0), values[0]),
                            new KeyFrame(Duration.millis(200), values[1]),
                            new KeyFrame(Duration.millis(400), values[2]),
                            new KeyFrame(Duration.millis(600), values[3]),
                            new KeyFrame(Duration.millis(800), values[4]),
                            new KeyFrame(Duration.millis(1000), values[5]),
                            new KeyFrame(Duration.millis(1200), values[6]),
                            new KeyFrame(Duration.millis(1400), values[7]),
                    };
                    timeline.getKeyFrames().addAll(frames);
                    timeline.setCycleCount(Timeline.INDEFINITE);
                    return true;
                }
            }
        return false;
    }

    /**
     * 添加分数
     *
     * @Description:
     * @Param: []
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void AddScore() {
        int temp = 0;
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                if (tableAfter[i][j] != 0) {
                    temp += (int) (Math.pow(2, table[i][j]));
                }
            }
        scoreInt += temp;
        score.setValue(String.format("%d", scoreInt));

    }

    /**
     * 失败判定
     *
     * @Description:
     * @Param: []
     * @return: boolean
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private boolean CheckLose() {
        int s = CheckEmptyArea().size();
        if (s == 0) {
            if (table[0][0] != 0)
                if (table[0][0] == table[0][1] || table[0][0] == table[1][0]) return false;
            if (table[0][size - 1] != 0)
                if (table[0][size - 1] == table[0][size - 2] || table[0][size - 1] == table[1][size - 1]) return false;
            if (table[size - 1][size - 1] != 0)
                if (table[size - 1][size - 1] == table[size - 1][size - 2] || table[size - 1][size - 1] == table[size - 2][size - 1])
                    return false;
            if (table[size - 1][0] != 0)
                if (table[size - 1][0] == table[size - 2][0] || table[size - 1][0] == table[size - 1][1]) return false;
            for (int i = 1; i < size - 1; i++) {
                if (table[i][0] != 0) {
                    if (table[i][0] == table[i - 1][0]
                            || table[i][0] == table[i + 1][0]
                            || table[i][0] == table[i][1])
                        return false;
                }
                if (table[i][size - 1] != 0) {
                    if (table[i][size - 1] == table[i - 1][size - 1]
                            || table[i][size - 1] == table[i + 1][size - 1]
                            || table[i][size - 1] == table[i][size - 2])
                        return false;
                }
                if (table[0][i] != 0) {
                    if (table[0][i] == table[0][i - 1]
                            || table[0][i] == table[0][i + 1]
                            || table[0][i] == table[1][i])
                        return false;
                }
                if (table[size - 1][i] != 0) {
                    if (table[size - 1][i] == table[size - 1][i - 1]
                            || table[size - 1][i] == table[size - 1][i + 1]
                            || table[size - 1][i] == table[size - 2][i])
                        return false;
                }
            }
            for (int i = 1; i < size - 1; i++)
                for (int j = 1; j < size - 1; j++) {
                    if (table[i][j] != 0) {
                        if (table[i][j] == table[i - 1][j]
                                || table[i][j] == table[i + 1][j]
                                || table[i][j] == table[i][j + 1]
                                || table[i][j] == table[i][j - 1])
                            return false;
                    }
                }
            return true;
        }
        return false;
    }

    /**
     * 处理
     *
     * @Description:
     * @Param: [event]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    @Override
    public void handle(KeyEvent event) {
        if (event.getEventType() == KeyEvent.KEY_PRESSED) {
            OnKeyPressed(event);
        } else if (event.getEventType() == KeyEvent.KEY_RELEASED) {
            OnKeyReleased(event);
        }
    }

    /**
     * 重置
     *
     * @Description:
     * @Param: [actionEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnReset(ActionEvent actionEvent) {
        ImplInit(size);
    }

    /**
     * 向下滑动
     *
     * @Description:
     * @Param: [swipeEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnSwipeDown(SwipeEvent swipeEvent) {
        ProcessCode(KeyCode.DOWN);
    }

    /**
     * 向左滑动
     *
     * @Description:
     * @Param: [swipeEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnSwipeLeft(SwipeEvent swipeEvent) {
        ProcessCode(KeyCode.LEFT);
    }

    /**
     * 向右滑动
     *
     * @Description:
     * @Param: [swipeEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnSwipeRight(SwipeEvent swipeEvent) {
        ProcessCode(KeyCode.RIGHT);
    }

    /**
     * 向右滑动
     *
     * @Description:
     * @Param: [swipeEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnSwipeUp(SwipeEvent swipeEvent) {
        ProcessCode(KeyCode.UP);
    }

    /**
     * 按下鼠标
     *
     * @Description:
     * @Param: [mouseEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnMousePressed(MouseEvent mouseEvent) {
        if (mouseEvent.getButton() == MouseButton.PRIMARY) {
            if (!mMousePressed) {
                mMousePressed = true;
                mPressedPoint = new Point((int) mouseEvent.getX(), (int) mouseEvent.getY());
            }
        }
    }

    /**
     * 释放鼠标
     *
     * @Description:
     * @Param: [mouseEvent]
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    public void OnMouseReleased(MouseEvent mouseEvent) {
        if (mouseEvent.getButton() == MouseButton.PRIMARY) {
            if (mMousePressed) {
                mMousePressed = false;
                mReleasedPoint = new Point((int) mouseEvent.getX(), (int) mouseEvent.getY());
                MouseManipulsation();
            }
        }

    }

    /**
     * 鼠标点击频率
     *
     * @Description:
     * @Param: []
     * @return: void
     * @Author: Yl-J
     * @Date: 2020/03/22
     */
    private void MouseManipulsation() {
        if (Math.abs(mPressedPoint.mx - mReleasedPoint.mx) < 50) {
            if (mReleasedPoint.my - mPressedPoint.my > 50) {
                ProcessCode(KeyCode.DOWN);
            } else if (mReleasedPoint.my - mPressedPoint.my < -50) {
                ProcessCode(KeyCode.UP);
            }
        }
        if (Math.abs(mPressedPoint.my - mReleasedPoint.my) < 50) {
            if (mReleasedPoint.mx - mPressedPoint.mx > 50) {
                ProcessCode(KeyCode.RIGHT);
            } else if (mReleasedPoint.mx - mPressedPoint.mx < -50) {
                ProcessCode(KeyCode.LEFT);
            }
        }
    }

}